<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class ExamquestionController extends BaseController {

    //考题列表
    public function index(){
        $data = array_filter($_GET);
        if (!empty($_GET['examquestiontype_title'])) {
            $data['examquestiontype_title'] = array('like',"%".trim($_GET['examquestiontype_title'])."%");
        }
        unset($data['p']);
        $data['examquestion_display'] = 1;
        $count = M('examquestion')->alias('e')->join('left join midi_examquestiontype as t on e.examquestion_examquestiontype_id = t.examquestiontype_id')->where($data)->count();
        $page = getpage($count,10);
        $examquestion_arr = M('examquestion')->alias('e')->join('left join midi_examquestiontype as t on e.examquestion_examquestiontype_id = t.examquestiontype_id')->where($data)->order('examquestion_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('examquestion_arr', $examquestion_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //添加考题
    public function examquestion_add(){
        if (I('post.action') == 'examquestion_add_action') {
            $this->examquestion_add_action();
            exit();
        }
        $examquestiontype_arr = M('examquestiontype')->select();
        $this->assign('examquestiontype_arr',$examquestiontype_arr);
        $this->display();
    }

    //添加考题操作
    public function examquestion_add_action(){
        if($_POST != array_filter($_POST)){
            $this->error('选项不能有空值');
        }
        //考试内容音频上传
        $ainfo = upload($_FILES['audio_file'],3);
        if ($ainfo['status'] == 0 && $ainfo['msg'] != 'nofile') {
            $this->error($ainfo['msg']);
        } elseif ($ainfo['status'] == 1) {
            $_POST['examquestion_file'] = $ainfo['filepath'];
        }
        if($_POST['examquestion_type'] == 1){
            $_POST['examquestion_answer'] = $_POST['examquestion_answer'][0];
        }elseif ($_POST['examquestion_type'] == 2) {
            $_POST['examquestion_answer'] = $_POST['examquestion_answer'][1];
        }else{
            $finfo = upload($_FILES['examquestion_answer'],3);
            //音频上传
            if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
                $this->error($finfo['msg']);
            } elseif ($finfo['status'] == 1) {
                $_POST['examquestion_answer'] = $finfo['filepath'];
            }
        }
        $_POST['examquestion_time'] = date('Y-m-d H:i:s',time());
        if(M('examquestion')->add($_POST)){
            $this->redirect('index');
        }else{
            $this->error('添加考题失败');
        }
    }

    //考题详情
    public function examquestion_details(){
        $examquestion_arr = M('examquestion')->alias('e')->join('left join midi_examquestiontype as t on e.examquestion_examquestiontype_id = t.examquestiontype_id')->where($_GET)->find();
        $this->assign('examquestion_arr',$examquestion_arr);
        $this->display();
    }

    //删除考题
    public function examquestion_del(){
        $_GET['examquestion_display'] = 0;
        if(M('examquestion')->save($_GET)){
            $this->redirect('index');
        }else{
            $this->error('删除考题失败！');
        }
    }

    //修改考题
    public function examquestion_edit(){
        if (I('post.action') == 'examquestion_edit_action') {
            $this->examquestion_edit_action();
            exit();
        }
        $examquestion_arr = M('examquestion')->alias('e')->join('left join midi_examquestiontype as t on e.examquestion_examquestiontype_id = t.examquestiontype_id')->where($_GET)->find();
        $this->assign('examquestion_arr',$examquestion_arr);
        $this->display();
    }

    //修改考题操作
    public function examquestion_edit_action(){
        if($_POST != array_filter($_POST)){
            $this->error('选项不能有空值');
        }
        if (!empty($_FILES['audio_file'])) {
            $ainfo = upload($_FILES['audio_file'],3);
            //音频上传
            if ($ainfo['status'] == 0 && $ainfo['msg'] != 'nofile') {
                $this->error($ainfo['msg']);
            } elseif ($ainfo['status'] == 1) {
                $_POST['examquestion_file'] = $ainfo['filepath'];
            }
        }
        if (!empty($_FILES['examquestion_answer'])) {
            $finfo = upload($_FILES['examquestion_answer'],3);
            //音频图标上传
            if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
                $this->error($finfo['msg']);
            } elseif ($finfo['status'] == 1) {
                $_POST['examquestion_answer'] = $finfo['filepath'];
            }
        }
        M('examquestion')->save($_POST);
        $this->redirect('index');
    }

}